//
//  RefundDetailViewController.m
//  B2C
//
//  Created by 李小龙 on 2017/8/17.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "RefundDetailViewController.h"
#import "OrderDetailTableViewCell.h"

@interface RefundDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation RefundDetailViewController
#pragma mark - UIViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"退款详情";
    
    [self.tableView clearExtendCellLine];
    [self getNetData];
}
#pragma mark - 网络请求
- (void)getNetData {
    // 根据订单id获取订单详细信息
    [MBProgressHUD showHUDProgress:self.view animated:YES];
    [MYAFNetworking POST:URL_Get_Order_Detail parameters:@{@"omid":self.detailModel.omid} success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject[@"code"] isEqualToString:@"200"]) {
            self.detailModel = [OrderModel mj_objectWithKeyValues:responseObject[@"data"]];
            [self.tableView reloadData];
        } else {
            [MBProgressHUD showError:self.view title:responseObject[@"message"] code:responseObject[@"code"] animated:YES];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showError:self.view title:@"" code:@"" animated:YES];
    }];
}
- (void)cancelRefund {
    [MBProgressHUD showHUDProgress:self.view animated:YES];
    [MYAFNetworking POST:URL_Delete_Refund parameters:@{@"omid":self.detailModel.omid} success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject[@"code"] isEqualToString:@"200"]) {
            [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"已提交取消申请" animated:YES];
            [self popToRootViewController];
        } else {
            [MBProgressHUD showError:self.view title:responseObject[@"message"] code:responseObject[@"code"] animated:YES];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showError:self.view title:@"" code:@"" animated:YES];
    }];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return 2;
        case 1:
            return self.detailModel.orderson.count+2;
        default:
            return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderDetailTableViewCell *cell = [OrderDetailTableViewCell refundDetailCellWith:tableView indexPath:indexPath count:self.detailModel.orderson.count];
    [cell updateRefundDetailCellWith:self.detailModel indexPath:indexPath count:self.detailModel.orderson.count];
    cell.detailBlock = ^(DetailCellClickType type) {
        // 取消退款
        NSLog(@"取消退款");
        if (type == DetailCellClickTypeRefund) {
            [self cancelRefund];
        }
    };
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:
            return 44;
        case 1:
            if (indexPath.row == self.detailModel.orderson.count) {
                return 42;
            } else if (indexPath.row == self.detailModel.orderson.count+1) {
                return UITableViewAutomaticDimension;
            } else {
                return 100;
            }
        default:
            return 0;
    }
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1 && indexPath.row == self.detailModel.orderson.count+1) {
        return 80;
    }
    return 44;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
